﻿using System;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace gms.system {
	public static class Database {
#region Utilities
		private static SqlConnection connection {
			get {
				return new SqlConnection(WebConfigurationManager.ConnectionStrings["GMSDB"].ConnectionString);
			}
		}
		private static void add_params(ref SqlDataAdapter cxt, object[] parameters) {
			if (parameters != null) {
				if (parameters.Length != 0) {
					for (int i = 0; i < parameters.Length; i++) {
						if (parameters[i] != null) {
							if (parameters[i].GetType().IsArray) {
								cxt.SelectCommand.Parameters.AddRange((SqlParameter[])parameters[i]);
							}
							else {
								cxt.SelectCommand.Parameters.Add((SqlParameter)parameters[i]);
							}
						}
					}
				}
			}
		}
		public static bool dataset_is_empty(DataSet data) {
			bool retval = true;

			if (data.Tables.Count != 0) {
				if (data.Tables[0].Rows.Count != 0) {
					retval = false;
				}
			}

			return retval;
		}
#endregion
#region Functions
		public static void exec(string proc_name, params object[] parameters) {
			SqlDataAdapter da = new SqlDataAdapter();

			da.SelectCommand = new SqlCommand();
			da.SelectCommand.Connection = connection;
			da.SelectCommand.CommandType = CommandType.StoredProcedure;
			da.SelectCommand.CommandText = proc_name;

			if (parameters != null) {
				da.SelectCommand.Parameters.AddRange(parameters);
			}

			try {
				da.SelectCommand.Connection.Open();
				da.SelectCommand.ExecuteNonQuery();
				da.SelectCommand.Connection.Dispose();
			}
			catch (Exception ex) {
				da.SelectCommand.Connection.Dispose();
				throw new Exception(ex.Message);
			}
		}
		public static DataSet exec_ds_return(string proc_name, params object[] parameters) {
			SqlDataAdapter da = new SqlDataAdapter();
			DataSet retval = new DataSet();

			da.SelectCommand = new SqlCommand();
			da.SelectCommand.Connection = connection;
			da.SelectCommand.CommandType = CommandType.StoredProcedure;
			da.SelectCommand.CommandText = proc_name;

			add_params(ref da, parameters);

			try {
				da.SelectCommand.Connection.Open();
				da.Fill(retval);
				da.SelectCommand.Connection.Close();
			}
			catch (Exception ex) {
				da.SelectCommand.Connection.Close();
			}

			return retval;
		}
		public static object exec_param_return(string proc_name, string param_name, params object[] parameters) {
			SqlDataAdapter da = new SqlDataAdapter();
			SqlParameter data = new SqlParameter();

			da.SelectCommand = new SqlCommand();
			da.SelectCommand.Connection = connection;
			da.SelectCommand.CommandType = CommandType.StoredProcedure;
			da.SelectCommand.CommandText = proc_name;

			add_params(ref da, parameters);

			data = new SqlParameter(param_name, null);
			da.SelectCommand.Parameters.Add(data);

			try {
				da.SelectCommand.Connection.Open();
				da.SelectCommand.ExecuteNonQuery();
				da.SelectCommand.Connection.Dispose();
			}
			catch (Exception ex) {
				da.SelectCommand.Connection.Dispose();
			}

			return data.Value;
		}
#endregion
	}
}